/*
 * Copyright (c) 2023, 2024,yzc.cn All rights reserved.
 *
 */
package cn.yzc.employeeListener;

import cn.yzc.mapper.StudentMapper;
import cn.yzc.pojo.Student;
import cn.yzc.server.StudentServer;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import org.apache.ibatis.annotations.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Map;

/**
 * <p>Project: EasyExcel - EmployeeListener</p>
 * <p>Powered by yzc On 2024-03-21 09:20:57</p>
 * <p>描述：<p>
 *
 * @author yzc [youzhicheng0415@163.com]
 * @version 1.0
 * @since 17
 */
@Component
//读监听器
public class EmployeeListener implements ReadListener<Student> {

    private int count = 100;
    private ArrayList<Student> list = new ArrayList<>(count);
    private StudentServer studentServer;

    public EmployeeListener(StudentServer studentServer) {
        this.studentServer = studentServer;
    }

    //读完一行就会调用这个方法
    @Override
    public void invoke(Student student, AnalysisContext analysisContext) {
        //将读取到的一行数据添加到集合中
        list.add(student);
        //判断是不是到达缓存量了
        if (list.size()>=100){
            studentServer.save(list);
            list = new ArrayList<>(count);
        }

    }

    //读完整个excel就会调用这个方法
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        //将余下的数据保存到数据库中
        if (list.size()>0) {
           studentServer.save(list);
            list = new ArrayList<>(count);

        }

    }
}
